/**
 * Source file: ConnectWebService.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id: Function_ConnectWebService.ejs 131 2013-04-29 22:31:05Z WillieVisagie55@gmail.com $
 * Date: $Date: 2013-04-30 00:31:05 +0200 (Tue, 30 Apr 2013) $
 */
 
//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in CheckPostal and AutoCompletes (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

 
function AccNoValidatorNew(AccountNoField,AccountTypeIdField,BranchIdField,BankIdField) {


	var AccountNo = EFormGetValue(AccountNoField);
	var AccountTypeId = EFormGetValue(AccountTypeIdField);
	var BranchId = EFormGetValue(BranchIdField);
	var BankId = EFormGetValue(BankIdField);
	//var BankName = EFormGetValue(BankNameField);

    if (AccountTypeId < 1 || AccountTypeId > 6) {
		alert("Please select an Account Type");
		return;
    }

    var parameters = '{"bankId":"' + BankId
                    + '","branchId":"' + BranchId
                    + '","accountTypeId":"' + AccountTypeId
                    + '","account":"' + AccountNo
		    + '"}';
    //alert(parameters);

    $.ajax({
        url: serverName+"/ACBServiceProxy.svc/ValidateAccount",
        type: 'POST',
        async: false,
        dataType: 'json',
        contentType: 'application/json; charset="utf-8"',
        data: parameters,
        complete: function (response) {
            //alert('complete: response ' + response + ' responseText ' + response.responseText);
            var res = jQuery.parseJSON(response.responseText);
            var errCode = parseInt(res.ValidateAccountResult);
            
                  
/*            ACB_SUCCESSFUL = 0,
            ACB_FAIL = -1,
            ACB_ACC_NOT_RECOGNISED = -5,
            ACB_NOT_CONNECTED = -10,
            ACB_INVALID_BANKCODE = -50,
            ACB_INVALID_BRANCHCODE = -51,
            ACB_REQUIRED_INFO = -52,
            ACB_BANK_DETAILS = -53,
            ACB_NON_COMPUTERISED = -54,
            ACB_NO_BANKCODE = -101,
            ACB_NO_BRANCHCODE = -102,
            ACB_NO_ACCTYPE = -103,
            ACB_SECURITY_FAIL = 999,
            ACB_VALIDATION_BANKID = 1010,
            ACB_VALIDATION_BRANCHID = 1020,
            ACB_VALIDATION_ACCOUNTNUM = 1030,
            ACB_VALIDATION_ACCOUNTYPE = 1040
*/            
	    var FocusField = AccountNoField; // default to account number
		
			if (errCode == 0) { // dont set focus when ok
				FocusField = "";
			} else if (errCode == -50 || errCode == -101 || errCode == 1010) { //ACB_INVALID_BANKCODE ACB_NO_BANKCODE ACB_VALIDATION_BANKID
            	FocusField = BankIdField;
            } else if (errCode == -51 || errCode == -102 || errCode == 1020) { //ACB_INVALID_BRANCHCODE ACB_NO_BRANCHCODE ACB_VALIDATION_BRANCHID            
            	FocusField = BranchIdField;
            } else if (errCode == -103 || errCode == 1040) { //ACB_NO_ACCTYPE ACB_VALIDATION_BRANCHID            
            	FocusField = AccountTypeIdField;
			}            

		
            GetErrorDescription(errCode,FocusField);
        },
        error: function (response) {
            alert('error ' + response.responseText);
        }

    });
}


function GetErrorDescription(errCode,FocusField) {

    var parameters = '{"errCode":"' + errCode + '"}';

    $.ajax({
        url: serverName+"/ACBServiceProxy.svc/ErrDescriptionString",
        type: 'POST',
        async: false,
        dataType: 'json',
        contentType: 'application/json; charset="utf-8"',
        data: parameters,
        complete: function (response) {
            //alert('complete: response ' + response + ' responseText ' + response.responseText);
            var res = jQuery.parseJSON(response.responseText);
            alert(res.ErrDescriptionStringResult);
			if (FocusField != "") {
				EFormSetFocus(EFormGetField(FocusField));
			}
        },
        error: function (response) {
            alert('error ' + response.responseText);
        }

    });
}